package base

import (
	"context"
	"net"
	"time"

	openGaussPQ "gitee.com/opengauss/openGauss-connector-go-pq"
	// pq "github.com/lib/pq"
	"golang.org/x/net/proxy"
)

// we wrap the proxy.Dialer to become dialer that the GaussDB module accepts
/*func newGaussProxyDialer(dialer proxy.Dialer) pq.Dialer {
	return &gaussProxyDialer{d: dialer}
}*/

func newGaussProxyDialer2(dialer proxy.Dialer) openGaussPQ.Dialer {
	return &gaussProxyDialer{d: dialer}
}

var _ openGaussPQ.Dialer = (&gaussProxyDialer{})

// gaussProxyDialer implements the GaussDB dialer using a proxy dialer, as their functions differ slightly
type gaussProxyDialer struct {
	d proxy.Dialer
}

// Dial uses the normal proxy dial function with the updated dialer
func (p *gaussProxyDialer) Dial(network, addr string) (c net.Conn, err error) {
	return p.d.Dial(network, addr)
}

// DialTimeout uses the normal GaussDB dial timeout function with the updated dialer
func (p *gaussProxyDialer) DialTimeout(network, address string, timeout time.Duration) (net.Conn, error) {
	ctx, cancel := context.WithTimeout(context.Background(), timeout)
	defer cancel()

	return p.d.(proxy.ContextDialer).DialContext(ctx, network, address)
}
